// run once 
function start() {
	var url = $("input[name='url']").val(),
		selector = $("input[name='selector']").val(),
		attrs = $("input[name='attrs']").val().split(",")
	output = $("input[name='output']").val();
	if (url == "" || selector == "" || attrs == "") {
		alert("Empty input,Kidding me?");
		return false;
	}

	jspider.get(url, selector, attrs, function(ret) {
		// parse output
		if (output != "") {
			var html = "";
			$.each(ret, function(i, item) {
				html += jspider.template(item, attrs, output);
			});
		} else {
			var html = JSON.stringify(ret);
		}
		$(".result").html(html);
	});
}

// in fact, you can use jspider.get more than once
// all these are aysnc
var root = "";

function start_multi() {
	var url = $("input[name='url']").val(),
		selector = $("input[name='selector']").val(),
		attrs = $("input[name='attrs']").val().split(",");
	if (url == "" || selector == "" || attrs == "") {
		alert("Empty input,Kidding me?");
		return false;
	}
	jspider.get(url, selector, attrs, function(ret) {
		$(".result").append(JSON.stringify(ret));
		var sub_url = "";
		var sub_selector = ".NewsEntity>h1";
		var sub_attrs = ["html"];
		root = jspider.root;
		$.each(ret, function(i, item) {
			sub_url = item.href;
			// handle urls relative to host
			(sub_url.indexOf("/") == 0) ? (sub_url = root + sub_url) : (sub_url = url + sub_url);
			jspider.get(sub_url, sub_selector, sub_attrs, function(ret) {
				$(".result").append(JSON.stringify(ret));
			})
		});
	});
}

// demo
function tryit() {
	var demo = {
		url: 'http://www.oschina.net/news/list/',
		selector: '#WeeklyNewsList a',
		attrs: 'href,html',
		output: "<a target='_blank' href='http://www.oschina.net/{{href}}'>{{html}}</a>"
	}
	$("input[name='url']").val(demo.url);
	$("input[name='selector']").val(demo.selector);
	$("input[name='attrs']").val(demo.attrs);
	$("input[name='output']").val(demo.output);
}